package com.storage.system.service.impl;

import com.storage.common.core.domain.AjaxResult;
import com.storage.common.core.domain.ServiceResponse;
import com.storage.common.core.page.TableDataInfo;
import com.storage.common.core.text.Convert;
import com.storage.common.enums.RoleType;
import com.storage.system.domain.StorageBrand;
import com.storage.system.domain.StorageUser;
import com.storage.system.mapper.StorageBrandMapper;
import com.storage.system.service.IStorageBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 品牌 服务层实现
 *
 * @author storage
 * @date 2019-08-18
 */
@Service
public class StorageBrandServiceImpl implements IStorageBrandService {
    @Autowired
    private StorageBrandMapper storageBrandMapper;

    /**
     * 查询品牌信息
     *
     * @param id 品牌ID
     * @return 品牌信息
     */
    @Override
    public StorageBrand selectStorageBrandById(Long id) {
        return storageBrandMapper.selectStorageBrandById(id);
    }

    /**
     * 查询品牌列表
     *
     * @param storageBrand 品牌信息
     * @return 品牌集合
     */
    @Override
    public ServiceResponse<TableDataInfo> selectStorageBrandList(StorageUser currentUser, StorageBrand storageBrand, Integer pageNum, Integer pageSize) {
        // 当前用户为店铺用户则使用shop作为创建者id
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        storageBrand.setCreateUserId(canOperationId);
        storageBrand.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));

        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        Integer start = (pageNum - 1) * pageSize;
        // 查询查询分页数据
        List<StorageBrand> users = storageBrandMapper.queryPageList(storageBrand, start, pageSize);
        // 查询totalCount
        Long totalCount = storageBrandMapper.queryPageCount(storageBrand);
        if (users == null) {
            return null;
        }

        TableDataInfo pageList = new TableDataInfo(users, totalCount, pageNum, pageSize);
        return new ServiceResponse<>(true, pageList, null);
    }

    /**
     * 新增品牌
     *
     * @param storageBrand 品牌信息
     * @return 结果
     */
    @Override
    public AjaxResult insertStorageBrand(StorageUser currentUser, StorageBrand storageBrand) {
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        Integer ownerType = RoleType.getOwnerType(currentUser.getRole());
        if (canOperationId == null) {
            return AjaxResult.error("当前用户不具备该操作权限");
        } else if (StringUtils.isBlank(storageBrand.getBrandName())) {
            return AjaxResult.error("品牌名称不能为空");
        } else if (storageBrandMapper.selectBrandCountByName(storageBrand.getBrandName(), canOperationId, ownerType) > 0) {
            return AjaxResult.error("已存在同名的品牌名称");
        }
        storageBrand.setCreateUserId(canOperationId);
        storageBrand.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));
        storageBrandMapper.insertStorageBrand(storageBrand);
        return AjaxResult.success(storageBrandMapper.selectStorageBrandById(storageBrand.getId()));
    }

    /**
     * 修改品牌
     *
     * @param storageBrand 品牌信息
     * @return 结果
     */
    @Override
    public AjaxResult updateStorageBrand(StorageUser currentUser, StorageBrand storageBrand) {
        Long canOperationId = RoleType.getOperationUserId(currentUser.getRole(), currentUser.getId(), currentUser.getShopId());
        if (canOperationId == null) {
            return AjaxResult.error("当前用不具备该操作权限");
        }
        storageBrand.setOwnerType(RoleType.getOwnerType(currentUser.getRole()));
        storageBrandMapper.updateStorageBrand(storageBrand);
        return AjaxResult.success(storageBrand);
    }

    /**
     * 删除品牌对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStorageBrandByIds(String ids) {
        return storageBrandMapper.deleteStorageBrandByIds(Convert.toStrArray(ids));
    }

}
